/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.scanner.helper;

import java.io.File;
import java.util.function.Function;
import java.util.stream.Stream;

import com.hyts.scanner.config.ScanRuleConfig;
import com.hyts.scanner.utility.PathUtils;
import com.hyts.scanner.utility.StringUtils;

/**
 * @title ClassHelper.java
 * @package com.hyts.apidoc.scanner.help
 * @description Class类帮助工具类操作
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com
 * @date 2018年4月20日
 * @version V1.0
 */
public final class ClassHelper {

	/**
	 * <p>
	 * 基础操作类
	 * </p>
	 * 
	 * @fieldName getBasePackage
	 * @fieldType Function<String,String>
	 */
	static Function<String, String> getBasePackage = (param) -> {
		if (StringUtils.isNotBlank(param)) {
			// 肯定为基包
			if (param.indexOf(".") < 0) {
				return param;
			} else {
				// 获取基包
				return param.substring(0, param.indexOf("."));
			}
		}
		return "";
	};

	/**
	 * <b>filePathToClass</b>
	 * <p>
	 * 文件路径直接转换为class加载器类
	 * </p>
	 * 
	 * @param filePath
	 * @param packages
	 * @return
	 * @exception
	 */
	public static Class filePathToClass(String filePath, String[] packages) {
		// 遍历基础包
		for (String packageName : packages) {
			if (filePath.contains(filePath.contains(File.separator)?PathUtils.package2Path(packageName):PathUtils.package2Path2(packageName))) {
				String classPath = filePath.substring(filePath
						.indexOf(filePath.contains(File.separator)?PathUtils.package2Path(packageName):PathUtils.package2Path2(packageName)));
				try {
					/*return Class.forName(classPath.contains(File.separator)?PathUtils.path2Package(classPath):PathUtils.path2Package2(classPath)
							.substring(
									0,
									(classPath.contains(File.separator)?PathUtils.path2Package(classPath):PathUtils.path2Package2(classPath))
											.lastIndexOf(".")));*/
					if(classPath.endsWith(".class")) {
						return Class.forName(classPath.contains(File.separator) ? PathUtils.path2Package(classPath).replace(".class", "") : PathUtils.path2Package2(classPath).replace(".class", ""));
					}
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					return null;
				}
			}
		}
		return null;
	}

	/**
	 * <b>TODO(方法功能的描述)</b>
	 * <p>com.hyts.scanner.base.BaseApiParser
	 * TODO(这里用一句话描述这个方法的作用)
	 * </p>
	 * 
	 * @param filePath
	 * @param packages
	 * @return
	 * @exception
	 */
	public static String filePathToUserPath(String filePath, String[] packages) {
		// 操作用户基础路径
		filePath = filePath.replace(File.separator, "/");
		for (String packageName : packages) {
			String packageNamePath = packageName.replace(".", "/");
			if (filePath.contains(packageNamePath)) {
				StringBuilder path = new StringBuilder();
				path.append(ScanRuleConfig.DEFAULT_PROJECT_USER_PATH)
						.append("/")
						.append(ScanRuleConfig.DEFAULT_PROJECT_TYPE_BASE)
						.append(filePath.substring(filePath
								.indexOf(packageNamePath)));
				return path.toString().replace(".class", ".java");
			}
		}
		return null;
	}

	/**
	 * <b>findBasePackage</b>
	 * <p>
	 * 获取基础包
	 * </p>
	 * 
	 * @param packages
	 * @return
	 * @exception
	 */
	public static Stream<String> findBasePackage(String[] packages) {
		// 长度排序 且进行去重
		return Stream.of(packages)/*
								 * .sorted((param1, param2) -> param1.length() -
								 * param2.length())
								 */
		.map(getBasePackage).distinct();
	}
}
